﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TByte.XnaHelper
{
    public class Polygon
    {
        #region Fields
        Line[] originalLines;
        Line[] transformedLines;

        #endregion

        #region Methods
        public bool Intersects(Polygon other)
        {
            foreach (Line thisLine in this.transformedLines)
            {
                foreach (Line otherLine in other.transformedLines)
                {
                    if (otherLine.Intersects(thisLine))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Line l in transformedLines)
            {
                l.draw(spriteBatch);
            }
        }
        public void Transform(float rotation, Vector2 translation)
        {
            Matrix transform = Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(new Vector3(translation.X, translation.Y, 0));

            transformedLines = new Line[originalLines.Length];

            for (int i = 0; i < originalLines.Length; i++)
            {

                transformedLines[i] = new Line(Vector2.Transform(originalLines[i].PointA, transform), Vector2.Transform(originalLines[i].PointB, transform));
            }
        }
        #endregion

        #region Constructors
        public Polygon(Line[] lines)
        {
            this.originalLines = lines;
            this.transformedLines = lines;
        }
        #endregion

        #region Properties
        public Line[] Lines { get { return transformedLines; } }
        #endregion
    }
}
